/* 
 * File:   create_events.c
 * Author: samik
 *
 * Created on 3 January, 2009, 7:57 PM
 */

#include <stdio.h>
#include <stdlib.h>

#include "simulator.h"

/*
 * 
 */
int main(int argc, char** argv) {
  FILE *fp[TOTAL_NO_MHS];
  char file[200] = "data/events.mh";
  char file1[200];

  int i;
  int no_of_events = 10; //simulation time
  int e; //e for event
  int h; //h for host
  int event_count[TOTAL_NO_MHS];

  if(argc>1) no_of_events=atoi(argv[1]);

  for(i=0;i<TOTAL_NO_MHS;i++){
    event_count[i]=0;
  }

  for(i=0;i<TOTAL_NO_MHS;i++){
    sprintf(file1, "%s%d", file,i);
    fp[i]=fopen(file1,"w");
  }

  /********************* EVENT GENERATION **********************/
  for (i = 1; i < TOTAL_NO_MHS; i++) {
    printf("Generating events for MH %d \nEvent sequence:", i);
    /* generate each MH's event sequence */
    while (event_count[i] < no_of_events) {
      e = rand() % 3 + 1; /*random number between 1 and 3*/
      switch (e) {
        case COMPUTE:
          fprintf(fp[i],"%s\n","compute ");
          printf(" COMPUTE ");
          break;
        case RECEIVE:
          //randomly choose an sender MH
          do {
            h = (rand() % (TOTAL_NO_MHS-1))+1;
          } while (h == i); //exclude current MH
          //add 'send msg' event to that mh
          fprintf(fp[h],"%s %d %s\n", "send", i, "DID-YOU-GET-THIS?");
          event_count[h]++;
          //add 'receive msg' event to current mh
          fprintf(fp[i], "%s %d\n", "receive", h);
          printf(" RECEIVE-MSG-FROM-%d\n", h);
          break;
        case SEND:
          //randomly choose a receiver MH
          do {
            h = (rand() % (TOTAL_NO_MHS-1))+1;
          } while (h == i); //exclude current MH
          //add 'send msg' event to current mh
          fprintf(fp[i],"%s %d %s\n", "send", h, "DID-YOU-GET-THIS?");
          //add 'receive msg' event to receiver mh
          fprintf(fp[h],"%s %d\n", "receive", i);
          event_count[h]++;
          printf(" SEND-MSG-TO-%d ", h);
          break;
      }
      event_count[i]++;
    }
    printf("\nEvent generation complete for mh - %d\n\n", i);
  }

  return (EXIT_SUCCESS);
}

